package cn.zbmao.web.controller.user;

import cn.zbmao.web.bean.User;
import cn.zbmao.web.interceptor.user.UserIntercepter;
import cn.zbmao.web.services.QualificationServices;
import cn.zbmao.web.util.StringOperateUtil;
import cn.zbmao.web.util.dao.DaoFactory;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import java.util.List;

public class QualificationController extends Controller {
    /**
     * 添加资质
     */
	@Before(UserIntercepter.class)
	public void add(){
        Integer qualificationId=0;
        String qualificationString=null;
        Integer quNum = getParaToInt("quNum");
        if (quNum==1){
            qualificationId = getParaToInt("qualificationId");
        }else if (quNum==0){
            qualificationString = getPara("qualificationId").replaceAll("[\\s]","");
            qualificationString = Jsoup.clean(StringOperateUtil.exChange(qualificationString), Whitelist.basic());
        }


		User user = (User)getSessionAttr("user");
//资质添加条件判断
		List<Record> list = DaoFactory.getUserDao().findUserQualificationAll(user.getInt("id"));
//        资质长度不超过5个
		if (list.size()>4) {
			setAttr("msg", 2);
//            资质重复
		}else if (quNum==1&&list.contains(new Record().set("qualification_id", qualificationId))) {
			setAttr("msg", 0);
//            添加成功
		}else if(quNum==1) {

                DaoFactory.getUserDao().addUserQualification(qualificationId, user.getInt("id"));
                setAttr("msg", 1);




        }else if(quNum==0){
//            资质输入为空
                if (StringUtils.isBlank(qualificationString)){
                    setAttr("msg",3);
                }else{
                    List<Record> qualifcationList= DaoFactory.getQualificationDao().findQualifcationName(qualificationString);
                    if (qualifcationList.size()>0){
                        qualificationId = qualifcationList.get(0).get("id");

                            DaoFactory.getUserDao().addUserQualification(qualificationId, user.getInt("id"));
                            setAttr("msg", 1);



                    }else{
//                       用户添加新资质
                        Record record = new Record().set("name",qualificationString)
                                .set("key",qualificationString.replaceAll("[^\\u4e00-\\u9fa51-9a-zA-Z]",""))
                                .set("is_ok",0).set("is_key",1).set("is_user",1);
                        DaoFactory.getQualificationDao().add(record);
                        qualificationId = Integer.parseInt(record.getLong("id").toString());


                        DaoFactory.getUserDao().addUserQualification(qualificationId, user.getInt("id"));
                        //清除缓存
                        QualificationServices.clear();

                        setAttr("msg", 1);
                    }
                }

        }else{
            setAttr("msg", 4);
        }

	
		render(new JsonRender().forIE());
	}

    /**
     * 资质删除
     */
	@Before(UserIntercepter.class)
	public void delete(){
        int id = getParaToInt();
        DaoFactory.getUserDao().deleteUserQualification(id);
		redirect("/news/condition");
	}
}
